package com.cheers.common;

import org.apache.log4j.Logger;

import cn.jpush.api.DeviceEnum;
import cn.jpush.api.ErrorCodeEnum;
import cn.jpush.api.JPushClient;
import cn.jpush.api.MessageResult;

public class JPushUtil {
	private static Logger logger = Logger.getLogger(JPushUtil.class);
	public static final int MAX = Integer.MAX_VALUE;
	public static final int MIN = (int) MAX / 2;
	public static final long TIMETOLIVE = 86400;// 默认1天

	private static final String masterSecret = "958056b7316d61746f2fac03";
	private static final String appKey= "2913c63ca5c0d646a48967ea";

	/**
	 * 保持 sendNo 的唯一性是有必要的 It is very important to keep sendNo unique.
	 * 
	 * @return sendNo
	 */
	public static int getRandomSendNo() {
		return (int) (MIN + Math.random() * (MAX - MIN));
	}

	/**
	 * 消息推送(Android,发送所有用户)
	 * 
	 * @param title
	 * @param content
	 * @return
	 */
	public static String sendMessage(String title, String content) {

		int sendNo = getRandomSendNo();

		// 对所有用户发送通知, 更多方法请参考文档
		JPushClient jpush = new JPushClient(masterSecret, appKey, TIMETOLIVE, DeviceEnum.Android);
		MessageResult msgResult = jpush.sendCustomMessageWithAppKey(sendNo, title, content);
		if (null != msgResult) {
			logger.info("服务器返回数据: " + msgResult.toString());
			if (msgResult.getErrcode() == ErrorCodeEnum.NOERROR.value()) {
				logger.info("发送成功， sendNo=" + msgResult.getSendno());
			} else {
				logger.info("发送失败， 错误代码=" + msgResult.getErrcode() + ", 错误消息=" + msgResult.getErrmsg());
			}
			return msgResult.toString();
		} else {
			logger.info("无法获取数据");
		}
		return "";
	}

	/**
	 * 消息推送(Android,发送给指定用户)
	 * 
	 * @param alias用户别名
	 * @param title
	 * @param content
	 * @return
	 */
	public static String sendMessageWithAlias(String alias, String title, String content) {

		int sendNo = getRandomSendNo();
		logger.info("JPushUtil==alias=" + alias + ";content=" + content);
		// 对所有用户发送通知, 更多方法请参考文档
		JPushClient jpush = new JPushClient(masterSecret, appKey, TIMETOLIVE, DeviceEnum.Android);
		MessageResult msgResult = null;
		try {
			msgResult = jpush.sendCustomMessageWithAlias(sendNo, alias, title, content);
			if (null != msgResult) {
				logger.info("alias为" + alias + ",content=" + content + "的消息，服务器返回数据: " + msgResult.toString());
				if (msgResult.getErrcode() == ErrorCodeEnum.NOERROR.value()) {
					logger.debug("alias为" + alias + "的JPUSH发送成功， sendNo=" + msgResult.getSendno());
				} else {
					logger.debug("alias为" + alias + "的JPUSH发送失败， 错误代码=" + msgResult.getErrcode() + ", 错误消息="
							+ msgResult.getErrmsg());
				}
			} else {
				logger.warn("alias为" + alias + "的JPush发送无法获取数据");
			}
		} catch (Exception e) {
			logger.error("alias为" + alias + "的Jpsuh消息提交，jpush服务器发送失败,异常信息" + e.getMessage());
		}
		if (msgResult == null) {
			msgResult = new MessageResult();
			msgResult.setErrcode(-1);
			msgResult.setErrmsg("alias为" + alias + "的Jpush消息发送失败");
		}
		return msgResult.toString();
	}

	/**
	 * 消息群发推送(Android,发送给指定用户)
	 * 
	 * @param alias
	 * @param title
	 * @param content
	 * @return
	 */
	public static String sendMessageWithTag(String tag, String title, String content) {

		int sendNo = getRandomSendNo();

		// 对所有用户发送通知, 更多方法请参考文档
		JPushClient jpush = new JPushClient(masterSecret, appKey, TIMETOLIVE, DeviceEnum.Android);
		MessageResult msgResult = jpush.sendCustomMessageWithTag(sendNo, tag, title, content);
		if (null != msgResult) {
			logger.info("服务器返回数据: " + msgResult.toString());
			if (msgResult.getErrcode() == ErrorCodeEnum.NOERROR.value()) {
				logger.info("发送成功， sendNo=" + msgResult.getSendno());
			} else {
				logger.info("发送失败， 错误代码=" + msgResult.getErrcode() + ", 错误消息=" + msgResult.getErrmsg());
			}
			return msgResult.toString();
		} else {
			logger.info("无法获取数据");
		}
		return "";
	}

}
